package com.newtouch.krd_app.view.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hjq.toast.ToastUtils;
import com.newtouch.krd_app.R;
import com.newtouch.krd_app.http.api.TerminalApi;
import com.newtouch.krd_app.util.CrashHandler;
import com.newtouch.krd_app.util.DeviceUtil;

import java.util.List;

/**
 * author : TQ
 * time   : 2023/2/14
 * desc   :
 */
public class PdfViewAdapter extends RecyclerView.Adapter<PdfViewAdapter.VHolder> {
    private List<TerminalApi.Bean.FilesDTO.PicFilesDTO> mDatas;

    public static class VHolder extends RecyclerView.ViewHolder{
        public ImageView imageView;
        public LinearLayout linearLayout;
        public VHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            linearLayout = itemView.findViewById(R.id.ll_item);
            ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();
            int screenWidth = DeviceUtil.getScreenWidth((Activity) itemView.getContext());
            int screenHeight = DeviceUtil.getScreenHeight((Activity) itemView.getContext());
            if (screenHeight>screenWidth){
                layoutParams.height = screenHeight/3;
            }
            CrashHandler.getInstance().towriteLog("viewsize:"+layoutParams.height+"X"+layoutParams.width);

        }
    }
    public PdfViewAdapter(List<TerminalApi.Bean.FilesDTO.PicFilesDTO> data) {
        this.mDatas = data;
    }
    @NonNull
    @Override
    public PdfViewAdapter.VHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from指定写法
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new VHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VHolder holder, int position) {
        Glide.with(holder.imageView.getContext())
                .load(mDatas.get(position).getFilePath())
                .skipMemoryCache(true)
                .into(holder.imageView);
        CrashHandler.getInstance().towriteLog("filepath:"+mDatas.get(position).getFilePath());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //item 点击事件
                ToastUtils.show(mDatas.get(position).getFilePath());
                CrashHandler.getInstance().towriteLog("filepath:"+mDatas.get(position).getFilePath()+"position"+position+"files:"+mDatas.size());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void setmDatas(List<TerminalApi.Bean.FilesDTO.PicFilesDTO> mDatas) {
        this.mDatas = mDatas;
    }
}
